package com.framework.dubbo.producer.file2html;

import cn.hutool.extra.spring.SpringUtil;
import com.framework.dubbo.producer.config.Constants;
import com.framework.dubbo.producer.utils.AlgorithmUtil;

import java.io.InputStream;

/**
 * 图片预览
 * @author xianggj
 * @Date 2021/6/8 12:43
 **/
public class ImgPreviewUtil extends AbstractFilePreviewTools {

    @Override
    public String convert2Html(String path) {
        return img2Html(path);
    }

    @Override
    public String convert2Html(InputStream in, String ext) {
        String filePath = writeToLocal(in, ext);
        return img2Html(filePath);
    }

    @Override
    public String convert2Str(InputStream in, String ext) {
        AlgorithmUtil algorithmUtil = SpringUtil.getBean(AlgorithmUtil.class);
        return algorithmUtil.ocr(in, System.currentTimeMillis() + Constants.DOT + ext);
    }

    public static String img2Html(String path) {
        StringBuffer sb = new StringBuffer();
        sb.append("<html>");
        sb.append("<head>");
        sb.append("<meta name='viewport' content='width=device-width, minimum-scale=0.1'>");
        sb.append("</head>");
        sb.append("<body style='margin: 0px; background: #0e0e0e;'>");
        sb.append("<div border='0' width='100%' style='margin:auto;text-align:center;padding: 120px;'>");
        String src = "<img style='-webkit-user-select: none;height:800px;' src='%s'> ";
        sb.append(String.format(src, path));
        sb.append("</div>");
        sb.append("</body>");
        sb.append("</html>");
        return sb.toString();
    }

}
